package com.bys.ots.util;

import com.bys.ots.config.RedisExceptionCache;
import com.bys.ots.handler.exception.RedisConnectionException;
import com.bys.ots.pojo.ResultEnum;
import com.bys.ots.pojo.ResultUtil;
import com.bys.ots.service.impl.UserServiceImpl;
import com.bys.ots.util.redis.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RedisWrapUtil
{
    @Autowired
    RedisUtil redisUtil;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    public boolean check(String checkName, String checkCode)
    {
        String res = "";
        try
        {
            res = redisUtil.get(checkName, 0);
        }
        catch (RedisConnectionException e)
        {
            try
            {
                res = RedisExceptionCache.backMap.get(checkName).toString();
            }
            catch (Exception e1)
            {
                return false;
            }
        }

        logger.info("验证码：res:" + res + "checkCode:" + checkCode);

        if (!StringUtils.equals(res, checkCode))
        {
            logger.error("->验证码不正确!, res: " + res + ", checkCode: " + checkCode);
            return false;
        }
        return true;
    }
}
